<?php
namespace app\space\controller;
use app\space\logic\SpaceLogic;

/**
 * 空间信息api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/25
 * Time: 09:21
 */
class Space extends Base {

    public function __construct(){
        parent::__construct();
        $this->s_logic = new SpaceLogic();
    }

    /**
     * 获取概述信息
     * @return \think\response\Json
     */
    public function ajax_get_overview(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->s_logic->get_search_ws_maps($params['branchid'],$params['floorid'],$params['use_status'],$params['work_type'],$params['keywords'],$params['expire_status']);
        $wsids = model('work_station')->get_column($maps,'id');

        //获取概述信息
        $s = new \app\common\controller\Space();
        $overview = $s->get_overview($wsids);

        $data['vacant_ws_num'] = $overview['vacant_ws_num'];
        $data['person_num'] = $overview['person_num'];
        $data['use_rate'] = $overview['use_rate'];
        $data['use_ws_num'] = $overview['use_ws_num'];
        return json($this->s_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取工位
     * @return \think\response\Json
     */
    public function ajax_get_ws(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $now_date = date('Y-m-d');
        $s = new \app\common\controller\Space();

        //处理空间工位“是否加班”显示
        $is_show_work_arr = $s->deal_space_is_show_work();

        //获取广告
        $maps = [];
        $maps[] = ['id','=',1];
        $a_detail = model('adv')->get_detail($maps,'pic');
        $adv_pic = !empty($a_detail['pic']) ? json_decode($a_detail['pic'],true) : [];

        $maps = $this->s_logic->get_search_ws_maps($params['branchid'],$params['floorid'],$params['use_status'],$params['work_type'],$params['keywords'],$params['expire_status']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $ws_list = model('work_station')->get_list($maps,'*','id desc','',$limit,$params['rows']);
        $list = [];
        foreach($ws_list as $k => $v){
            $ws_list[$k]['work_type_zw'] = $v->work_type_name;
            $ws_list[$k]['price'] = deal_number_div_100($v['price']);
            $ws_list[$k]['is_indoor_air_zw'] = $v->is_indoor_air_name;
            $ws_list[$k]['ws_pic'] = !empty($v['ws_pic']) ? json_decode($v['ws_pic'],true) : [];
            $ws_list[$k]['company_pic'] = !empty($v['company_pic']) ? json_decode($v['company_pic'],true) : [];
            $ws_list[$k]['company_work_preson'] = !empty($v['company_work_preson']) ? json_decode($v['company_work_preson'],true) : [];

            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
            $ws_list[$k]['region_zw'] = $r_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $ws_list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $ws_list[$k]['branch_zw'] = $b_detail['name'];

            //获取空间工位信息
            $cp_ws_info = $s->get_space_ws_info($v['regionid'],$v['id'],$v['company_out_time']);
            $ws_list[$k]['use_status'] = $cp_ws_info['use_status'];
            $ws_list[$k]['use_status_zw'] = $cp_ws_info['use_status_zw'];
            $ws_list[$k]['use_rate'] = $cp_ws_info['use_rate'];
            $ws_list[$k]['person_num'] = $cp_ws_info['person_num'];
            $ws_list[$k]['is_holiday_work'] = $cp_ws_info['is_holiday_work'];
            $ws_list[$k]['is_evening_work'] = $cp_ws_info['is_evening_work'];
            $ws_list[$k]['ccids'] = $cp_ws_info['ccids'];
            $ws_list[$k]['remark'] = $cp_ws_info['remark'];
            $ws_list[$k]['status'] = $cp_ws_info['status'];
            $ws_list[$k]['status_zw'] = $cp_ws_info['status_zw'];

            //计算剩余天数
            $ws_list[$k]['rest_day'] = get_time_diff_day($v['company_out_time'],$now_date);

            //计算空置天数
            $ws_list[$k]['vacant_day'] = get_time_diff_day($now_date,$v['company_out_time']);

            //是否显示“晚上是否加班”
            $ws_list[$k]['is_show_evening_work'] = $is_show_work_arr['is_show_evening_work'];
            //是否显示“周末/节假日是否加班”
            $ws_list[$k]['is_show_holiday_work'] = $is_show_work_arr['is_show_holiday_work'];

            //计算入住年限
            //$r = get_time_diff_date($v['company_in_time'],$now_date);
            //$ws_list[$k]['company_in_diff_date'] = $r['year'] . '年' . $r['month'] . '月';

            $ws_list[$k]['adv_pic'] = $adv_pic;

            $list[$k] = $v->toArray();
        }

        //获取社区巡店工位客户交流
        $maps = [];
        $maps[] = ['key','=','cp_ws_cc'];
        $conf_info = $this->s_logic->get_conf_info($maps);
        $conf_info['cp_ws_cc'] = json_decode($conf_info['cp_ws_cc'],true);

        $data['list'] = $list;
        $data['page'] = $params['page'];
        $data['conf_info'] = $conf_info;
        return json($this->s_logic->get_response(1,'OK',$data));
    }

    /**
     * 添加空间工位信息
     * @return \think\response\Json
     */
    public function ajax_add_ws(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->s_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $s = new \app\common\controller\Space();

        //查找工位
        $r_maps = [];
        $r_maps[] = ['id','=',$params['wsid']];
        $r_detail = model('work_station')->get_detail($r_maps,'id,regionid');

        //查找区域
        $r_maps = [];
        $r_maps[] = ['id','=',$r_detail['regionid']];
        $r_detail = model('region')->get_detail($r_maps,'id,floorid');

        //查找楼层
        $f_maps = [];
        $f_maps[] = ['id','=',$r_detail['floorid']];
        $f_detail = model('floor')->get_detail($f_maps,'id,branchid');

        //查找分部
        $b_maps = [];
        $b_maps[] = ['id','=',$f_detail['branchid']];
        $b_detail = model('branch')->get_detail($b_maps,'id');

        $ws_data = [];
        $ws_data['openid'] = $u_detail['openid'];
        $ws_data['cp_type'] = 1;
        $ws_data['cp_branchid'] = 0;
        $ws_data['cp_floorid'] = 0;
        $ws_data['branchid'] = $b_detail['id'];
        $ws_data['floorid'] = $f_detail['id'];
        $ws_data['regionid'] = $r_detail['id'];
        $ws_data['wsid'] = $params['wsid'];
        $ws_data['use_status'] = $params['use_status'];
        $ws_data['use_rate'] = trim($params['use_rate']);
        $ws_data['person_num'] = trim($params['person_num']);
        $ws_data['is_holiday_work'] = $params['is_holiday_work'];
        $ws_data['is_evening_work'] = $params['is_evening_work'];
        $ws_data['ccids'] = json_encode($params['ccids'],JSON_UNESCAPED_UNICODE);
        $ws_data['remark'] = trim($params['remark']);
        $ws_data['status'] = 1;

        $crwid = model('cp_region_ws')->insertGetId($ws_data);

        //更新写备忘录积分
        $si = new \app\common\controller\SaveIntegral();
        $r = $si->save_wr_integral($crwid);

        //计算空间工位使用率
        $use_rate_arr = $s->count_space_ws_use_rate(1,$params['wsid']);
        $ws_data = [];
        $ws_data['use_rate'] = $use_rate_arr['use_rate'];

        $maps = [];
        $maps[] = ['id','=',$crwid];
        $r = model('cp_region_ws')->force()->save($ws_data,$maps);
        return json($this->s_logic->get_response(1,'添加成功'));
    }

    /**
     * 获取工位详情
     * @return \think\response\Json
     */
    public function ajax_get_ws_detail(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $now_date = date('Y-m-d');
        $s = new \app\common\controller\Space();

        //处理空间工位“是否加班”显示
        $is_show_work_arr = $s->deal_space_is_show_work();

        //检查获取工位详情规则
        $r = $this->s_logic->check_get_ws_detail_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $ws_detail['work_type_zw'] = $ws_detail->work_type_name;
        $ws_detail['price'] = deal_number_div_100($ws_detail['price']);
        $ws_detail['is_indoor_air_zw'] = $ws_detail->is_indoor_air_name;
        $ws_detail['ws_pic'] = !empty($ws_detail['ws_pic']) ? json_decode($ws_detail['ws_pic'],true) : [];
        $ws_detail['company_pic'] = !empty($ws_detail['company_pic']) ? json_decode($ws_detail['company_pic'],true) : [];
        $ws_detail['company_work_preson'] = !empty($ws_detail['company_work_preson']) ? json_decode($ws_detail['company_work_preson'],true) : [];

        //查找区域
        $r_maps = [];
        $r_maps[] = ['id','=',$ws_detail['regionid']];
        $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
        $ws_detail['region_zw'] = $r_detail['name'];

        //查找楼层
        $f_maps = [];
        $f_maps[] = ['id','=',$r_detail['floorid']];
        $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
        $ws_detail['floor_zw'] = $f_detail['name'];

        //查找分部
        $b_maps = [];
        $b_maps[] = ['id','=',$f_detail['branchid']];
        $b_detail = model('branch')->get_detail($b_maps,'id,name');
        $ws_detail['branch_zw'] = $b_detail['name'];

        //获取空间工位信息
        $cp_ws_info = $s->get_space_ws_info($ws_detail['regionid'],$ws_detail['id'],$ws_detail['company_out_time']);
        $ws_detail['use_status'] = $cp_ws_info['use_status'];
        $ws_detail['use_status_zw'] = $cp_ws_info['use_status_zw'];
        $ws_detail['use_rate'] = $cp_ws_info['use_rate'];
        $ws_detail['person_num'] = $cp_ws_info['person_num'];
        $ws_detail['is_holiday_work'] = $cp_ws_info['is_holiday_work'];
        $ws_detail['is_evening_work'] = $cp_ws_info['is_evening_work'];
        $ws_detail['ccids'] = $cp_ws_info['ccids'];
        $ws_detail['remark'] = $cp_ws_info['remark'];
        $ws_detail['status'] = $cp_ws_info['status'];
        $ws_detail['status_zw'] = $cp_ws_info['status_zw'];

        //计算剩余天数
        $ws_detail['rest_day'] = get_time_diff_day($ws_detail['company_out_time'],$now_date);

        //计算空置天数
        $ws_detail['vacant_day'] = get_time_diff_day($now_date,$ws_detail['company_out_time']);

        //是否显示“晚上是否加班”
        $ws_detail['is_show_evening_work'] = $is_show_work_arr['is_show_evening_work'];
        //是否显示“周末/节假日是否加班”
        $ws_detail['is_show_holiday_work'] = $is_show_work_arr['is_show_holiday_work'];

        //计算入住年限
        //$r = get_time_diff_date($ws_detail['company_in_time'],$now_date);
        //$ws_detail['company_in_diff_date'] = $r['year'] . '年' . $r['month'] . '月';

        //获取社区巡店工位客户交流
        $maps = [];
        $maps[] = ['key','=','cp_ws_cc'];
        $conf_info = $this->s_logic->get_conf_info($maps);
        $conf_info['cp_ws_cc'] = json_decode($conf_info['cp_ws_cc'],true);

        //获取广告
        $maps = [];
        $maps[] = ['id','=',1];
        $a_detail = model('adv')->get_detail($maps,'pic');
        $adv_pic = !empty($a_detail['pic']) ? json_decode($a_detail['pic'],true) : [];
        $ws_detail['adv_pic'] = $adv_pic;

        $data['detail'] = $ws_detail;
        $data['conf_info'] = $conf_info;
        return json($this->s_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取工位留言
     * @return \think\response\Json
     */
    public function ajax_get_wsm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->s_logic->get_search_wsm_maps($params);

        $list = model('work_station_message')->get_list($maps,'id,openid,message,c_time','id desc');
        foreach($list as $k => $v){
            //获取微信用户信息
            $v_maps = [];
            $v_maps[] = ['openid','=',$v['openid']];
            $v_detail = model('visitor')->get_detail($v_maps,'id,nickname,headimgurl');
            $list[$k]['nickname'] = !empty($v_detail) ? $v_detail['nickname'] : '';
            $list[$k]['headimgurl'] = !empty($v_detail) ? $v_detail['headimgurl'] : '';
        }

        $data['list'] = $list;
        return json($this->s_logic->get_response(1,'Ok',$data));
    }

    /**
     * 添加工位留言
     * @return \think\response\Json
     */
    public function ajax_add_wsm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }
        $openid = session('openid');

        //检查添加工位留言规则
        $r = $this->s_logic->check_add_wsm_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $wsm_data = [];
        $wsm_data['openid'] = $openid;
        $wsm_data['wsid'] = $ws_detail['id'];
        $wsm_data['message'] = $msg = trim($params['message']);
        $r = model('work_station_message')->insert($wsm_data);
        if($r){
            $sm = new \app\common\controller\SendMsg();

            $maps = [];
            $maps[] = ['openid','=',$openid];
            $u_detail = model('user')->get_detail($maps,'id,username,tel');
            $username = !empty($u_detail['username']) ? $u_detail['username'] : '无';
            $tel = !empty($u_detail['tel']) ? $u_detail['tel'] : '无';

            $wsid = $ws_detail['id'];
            $ws_str = $ws_detail['branch_zw'] . '-' . $ws_detail['floor_zw'] . '-' . $ws_detail['number'];
            $c_time = date('Y-m-d H:i:s');

            //查找分部工作人员
            $maps = [];
            $maps[] = ['department','=',$ws_detail['branch_zw']];
            $maps[] = ['status','=',1];
            $maps[] = ['enable','=',1];
            $openids_1 = model('user')->get_column($maps,'openid');

            //查找公司负责人
            $maps = [];
            $maps[] = ['tel','=',$ws_detail['company_tel']];
            $maps[] = ['status','=',1];
            $maps[] = ['enable','=',1];
            $openids_2 = model('user')->get_column($maps,'openid');

            $openids = array_merge($openids_1,$openids_2);
            $openids = array_unique($openids);
            foreach($openids as $k => $v){
                //发送工位留言消息
                $r = $sm->send_wsm_msg($v,$username,$tel,$msg,$c_time,$wsid,$ws_str);
            }

            return json($this->s_logic->get_response(1,'留言成功'));
        }
        return json($this->s_logic->get_response(0,'留言失败'));
    }
}
